package com.example.springboot.redis.demo.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.Sort;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderConsumer {

    /**
     * 消费者
     * @param order
     * @param channel
     * @param tag
     * @throws IOException
     */
    @RabbitListener(
        queues = "order.queue",
        containerFactory = "customContainerFactory"
    )
    public void handleOrder(Order order, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        try {
            // 处理业务逻辑
            processOrder(order);
            // 手动确认消息（multiple=false）
            channel.basicAck(tag, false);
        } catch (Exception e) {
            // 处理失败：拒绝消息并重新入队（requeue=false 时进入死信队列）
            channel.basicNack(tag, false, false);
        }
    }

    private void processOrder(Order order) {
        // 业务处理逻辑
    }
}